package com.zekdot.password.action;

import com.zekdot.password.dto.Envelope;
import com.zekdot.password.entity.UserEntity;
import com.zekdot.password.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpSession;

/**
 * Create by zekdot on 19-12-29.
 */
@Controller
@RequestMapping("/user")
public class UserAction {

    @Autowired
    private UserService userService;

    @RequestMapping(value = "/login",method = RequestMethod.POST)
    @ResponseBody
    public String login(UserEntity userEntity, HttpSession session){
        try {
            Envelope envelope = userService.login(userEntity);
            if(envelope.getCode() == 0){
                session.setAttribute("user",envelope.getObj());
                envelope.setObj(null);
            }
            return envelope.toString();
        }catch (Exception e){
            return Envelope.systemError.toString();
        }

    }
}
